﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLBS.Data;
using NLBS.Entity;
using System.Web.UI.HtmlControls;
using System.Web;
using NLBS.Framework;
using NLBS.Text;
using NLBS.Web;
using NLBS.Data.Business;

namespace NLBS.Control
{
   public class PanelArticle: PanelModule
    {
       [TemplateContainer(typeof(DefaultArticleTemplate))]
        [PersistenceMode(PersistenceMode.InnerProperty)]
        public override ITemplate ItemTemplate
        {
            get
            {
                return base.ItemTemplate;
            }
            set
            {
                base.ItemTemplate = value;
            }
        }

       public override string Title
       {
           get
           {
               return lang["recent_articles"];
           }
           set
           {
               base.Title = value;
           }
       }

        protected override void CreateChildControls()
        {
            if (ItemTemplate == null)
                ItemTemplate = new DefaultArticleTemplate();
            base.CreateChildControls();
        }

    }

   public class DefaultArticleTemplate : ITemplate
   {
       public void InstantiateIn(System.Web.UI.Control container)
       {
           container.Controls.Add(new LiteralControl("<ul>"));
           IList<Article> artList =ArticleData.GetTopArticle(BlogInfo.Instance().RecentArticleList);
           if (artList != null)
           {
               int j=0;
               for (int i = 0; i < artList.Count && j < BlogInfo.Instance().RecentArticleList; i++)
               {

                  bool viewPower=ViewHelp.checkViewPermission(artList[i]);
                  if (viewPower)
                  {
                      j++;
                      Article article = artList[i];
                      container.Controls.Add(new LiteralControl("<li>"));
                      string txt = string.Format("<a href=\"{0}\" onclick=\"{1}\" title=\"{3}\">{2}</a>",
                          ReWriteUrls.Instance().ArticleConent(article.ArticleID),
                          UrlFormatsProvider.Instance().ArticleConent(article.ArticleID),
                          StringHelper.SubStr(15, article.Title), article.Title);
                      container.Controls.Add(new LiteralControl(txt));
                      container.Controls.Add(new LiteralControl("</li>"));
                  }
                  
               }
               
           }
           container.Controls.Add(new LiteralControl("</ul>"));
       }
      
   }
}
